import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

public class ElasticsearchDownloadExample {
    public static void main(String[] args) throws IOException {
        // 初始化Elasticsearch客户端
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 创建请求
        GetRequest request = new GetRequest("imgstore", "1732867275178");
        // 客户端发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsString());
        JSONObject imgObject = JSON.parseObject(response.getSourceAsString());
        byte[] imageData = Base64.getDecoder().decode(imgObject.get("blob").toString());

        FileOutputStream fos = new FileOutputStream("1732867275178.png");
        fos.write(imageData);
        // 关闭客户端
        client.close();
    }
}
